#include<iostream>
#include"../comm/httplib.h"
#include"oj_control.hpp"

using namespace httplib;
using namespace ns_control;
int main()
{
    //用户请求路由的功能
    Server svr;
    Control ctrl;

    //获取所有题目列表
   svr.Get("/all_questions",[&ctrl](const Request&req,Response&resp){
       string html;
       ctrl.AllQuestions(&html);
       resp.set_content(html,"text/html;charset=utf-8");

   });
    //用户根据题目的编号，获取题目内容
    //R"()",保持字符串原貌，不用转义
    //   /question/100->  正则匹配
    svr.Get(R"(/questions/(\d+))",[&ctrl](const Request&req,Response&resp){
        std::string number=req.matches[1];
        std::string html;
        ctrl.Question(number,&html);
        resp.set_content(html,"text/html;charset=utf-8");
    });
    //用户提交代码，使用判题功能（1.每道题的测试用例2.compile_and_run）
    svr.Post(R"(/judge/(\d+))",[&ctrl](const Request&req,Response&resp){
        std::string number=req.matches[1];
        std::string result_json;
        ctrl.Judge(number,req.body,&result_json);
        resp.set_content(result_json,"application/json;charset=utf-8");
       // resp.set_content("指定题目判题: "+number,"text/plain;charset=utf-8");
        
    });
    svr.set_base_dir("./wwwroot");
    svr.listen("0.0.0.0",9000);


    return 0;
}